//
//  RHMaskView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/19.
//

#import "RHMaskView.h"
#import "DeviceChartFieldModel.h"
#import "NSNumber+SYAdd.h"

@interface RHMaskView ()




@end
@implementation RHMaskView
- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}
- (instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}




-(void)updateModel:(DevChartModel*)chartModel index:(NSInteger)x colors:(nonnull NSArray *)colorArr;
{
    _chartModel = chartModel;
//    NSLog(@"----->chart model count:%ld",self.chartModel.selectArray.count);
    [self qmui_removeAllSubviews];
    __block CGFloat h = 20;
    __block CGFloat w = 18;
    QMUILabel *timeLab = [[QMUILabel alloc]init];
    timeLab.text = chartModel.xTitlesArray[x];
    timeLab.textColor = [UIColor whiteColor];
    timeLab.font = [UIFont systemFontOfSize:14];
    timeLab.textAlignment = NSTextAlignmentLeft;
    [self addSubview:timeLab];
    
    [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(5);
    }];
    
    NSMutableArray *arr = [NSMutableArray array];
    [_chartModel.selectArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel  *_Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(model.isShowLine){
            h += 20;
            
            UIView *contentView = kView;
            [self addSubview:contentView];
            
            UILabel *point = [[QMUILabel alloc]init];
            point.backgroundColor = kCOLOR_HEX(colorArr[idx%colorArr.count]);
            point.layer.cornerRadius = 4;
            point.layer.masksToBounds = YES;
            [contentView addSubview:point];
            
            UILabel *keyLab = [[QMUILabel alloc]init];
            keyLab.text = [NSString stringWithFormat:@"%@:",model.alias.rh_localized];
            keyLab.textColor = UIColor.whiteColor;
            keyLab.font = [UIFont systemFontOfSize:12];
            [contentView addSubview:keyLab];
            
            //
            NSString *value = [NSDecimalNumber decimalStringWithDouble:[model.dataArray[x] doubleValue]];
            UILabel *contentLab = [[QMUILabel alloc]init];
            contentLab.text = [NSString stringWithFormat:@"%@%@",value,model.unitName];
            contentLab.textAlignment = NSTextAlignmentLeft;
            contentLab.textColor = UIColor.whiteColor;
            contentLab.font = [UIFont systemFontOfSize:12];
            [contentView addSubview:contentLab];
            if (model.hasNaNValue) {
                contentLab.text = @"NaN";
            }
            
            [keyLab sizeToFit];
            [contentLab sizeToFit];
            w = MAX(keyLab.width + contentLab.width, w);
            
            
            [arr addObject:contentView];
            
            [point mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(5);
                make.width.height.offset(8);
            }];
            
            [keyLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(point.mas_right).offset(5);
                make.top.bottom.mas_offset(0);
                make.centerY.equalTo(point);
            }];
            
            [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(keyLab.mas_right);
                make.centerY.equalTo(keyLab);
               
            }];
            
            [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
            
            }];
        }
       
    }];
    kWeakSelf;
    //
    if (arr.count>1) {
        [arr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:5 leadSpacing:25 tailSpacing:5];
    }
    if(arr.count == 1){
        UIView *itemView = (UIView*)[arr firstObject];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(timeLab.mas_bottom).offset(0);
            make.bottom.mas_offset(-5);
        }];
    }
    
    
    self.height = h;
    self.width = w + 18 + 5;
    [self setNeedsLayout];
    [self layoutIfNeeded];
    [self setNeedsDisplay];
}

-(void)setupUI
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    
}

@end
